package com.example.familylibrary.controller;

import com.example.familylibrary.dto.ReadingNoteDTO;
import com.example.familylibrary.service.ReadingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/notes")
public class ReadingNoteController {

    @Autowired
    private ReadingNoteService readingNoteService;

    @PostMapping
    public ResponseEntity<Map<String, Object>> createNote(@RequestBody ReadingNoteDTO readingNoteDTO) {
        try {
            ReadingNoteDTO note = readingNoteService.createNote(readingNoteDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "阅读想法创建成功");
            response.put("data", note);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getNoteById(@PathVariable Long id) {
        try {
            ReadingNoteDTO note = readingNoteService.getNoteById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", note);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/book/{bookId}")
    public ResponseEntity<Map<String, Object>> getNotesByBookId(
            @PathVariable Long bookId,
            @RequestParam Long userId) {
        List<ReadingNoteDTO> notes = readingNoteService.getNotesByUserAndBook(userId, bookId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", notes);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> getNotesByUserId(@PathVariable Long userId) {
        List<ReadingNoteDTO> notes = readingNoteService.getNotesByUserId(userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", notes);
        return ResponseEntity.ok(response);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateNote(@PathVariable Long id, @RequestBody ReadingNoteDTO readingNoteDTO) {
        try {
            ReadingNoteDTO updatedNote = readingNoteService.updateNote(id, readingNoteDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "阅读想法更新成功");
            response.put("data", updatedNote);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteNote(@PathVariable Long id) {
        try {
            readingNoteService.deleteNote(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "阅读想法删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}